package algorithm.linklist;

/**
 * @ Author     ：wangyw
 * @ Date       ：Created in 2:52 下午 2021/10/28
 * @ Description：链表实现堆栈：先进后出
 */
public class LinkQueue {

    Node head;  // 头
    Node tail;  // 尾
    int size;

    public void offer(int v) {
        Node node = new Node(v);
        if (tail == null) {
            head = node;
            tail = node;
        } else {
            tail.next = node;
            tail = tail.next;
        }
        size++;
    }
    // 检索并取出head
    public int poll() {
        int ans = -1;
        if (head == null) {
            return -1;
        } else {
            ans = head.val;
            head = head.next;
        }
        if (head == null) {
            tail = null;
        }
        size--;
        return ans;
    }

    // 检索head
    public int seek() {
        int ans = -1;
        if (head == null) {
            return -1;
        } else {
            ans = head.val;
        }
        return ans;
    }


    public static void main(String[] args) {
        LinkQueue lq = new LinkQueue();
        lq.offer(11);
        lq.offer(2);
        System.out.println("lq.poll() = " + lq.poll());
        System.out.println("lq.seek() = " + lq.seek());
        System.out.println("lq.poll() = " + lq.poll());
    }
}
